package com.luckycoder.controller;

import com.luckycoder.entity.Student;
import com.luckycoder.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@RestController
public class StudentHandler {

    @Autowired
    private StudentRepository studentRepository;

    @GetMapping("/findAll")
    public Collection<Student> findAll() {
        return studentRepository.findAll();
    }

    @GetMapping("/findById/{id}")
    public Student findById( @PathVariable("id") long id) {
        return studentRepository.findById(id);
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") Long id) {
        studentRepository.delete(id);
    }

    @PutMapping("/update")
    public void Update(Student student) {
        studentRepository.saveOrUpdate(student);
    }

    @PostMapping("/save")
    public void save(Student student) {
        studentRepository.saveOrUpdate(student);
    }
}
